const baseUrl = "https://zhxfcpxcx.nz-cloud.com";
// const baseUrl = "https://zhxfcpxcx.nz-cloud.com";
import { useStore } from '@/stores/user';
type Method = 'GET' | 'POST'
export const request = (url : string, data : any, method : Method) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中',
			icon: 'loading',
			mask: true
		});
		uni.request({
			url: `${baseUrl}/${url}`,
			method: method,
			data: data,
			header: {
				'api-token': useStore().token
			},
			success: (res : any) => {
				if (res.data.message === '头部api-token错误') {
					uni.showToast({
						title: '登录超时,请重新登录',
						icon: 'none'
					})
					useStore().logout()
				}
				if (data.mobile) {
					resolve(res)
				} else {
					if (res.data.code == 1000) {
						resolve(res.data)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
						return 
					}
				}
				uni.hideLoading(); //关闭loading
			},
			fail: err => {
				reject(err);
				uni.hideLoading(); //关闭loading
			}
		});
	})
}
export const url = baseUrl

export const generateRandomString = (length : number) => {
	const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	let randomString = '';

	while (randomString.length < length) {
		const randomChar = characters.charAt(Math.floor(Math.random() * characters.length));

		// 检查生成的字符是否以数字开头，如果是，则继续生成下一个字符
		if (randomString.length === 0 && /\d/.test(randomChar)) {
			continue;
		}

		randomString += randomChar;
	}

	return randomString;
}

export const convert2TecentMap = (lng : any, lat : any) => {
	if (lng == '' && lat == '') {
		return {
			lng: '',
			lat: ''
		}
	}
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0
	var x = lng - 0.0065
	var y = lat - 0.006
	var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
	var qqlng = z * Math.cos(theta)
	var qqlat = z * Math.sin(theta)
	return {
		lng: qqlng,
		lat: qqlat
	}
}